﻿using DeviceMaintainGIS.Entity;
using DeviceMaintainGIS.Model;
using DeviceMaintainGIS.Model.ParInfo;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DeviceMaintainGIS.BLL.Pro
{
    /// <summary>
    /// 项目操作
    /// </summary>
    public class ProService
    {
        /// <summary>
        /// 根据ID查询项目信息
        /// </summary>
        /// <param name="ProjectGuid"></param>
        /// <returns></returns>
        public ReturnVM<ProInfoModel> GetProListByID(string ProjectGuid)
        {
            ReturnVM<ProInfoModel> model = new ReturnVM<ProInfoModel>();
            using (CYOADBEntities Entities = new CYOADBEntities())
            {
                var linq = from a in Entities.ProInfo_Tab.Where(i => i.ProjectGuid == ProjectGuid)
                           select new ProInfoModel
                           {
                               ProjectGuid = a.ProjectGuid,
                               ProjectId = a.ProjectId,
                               ProjectName = a.ProjectName,
                               ProjectManager = a.ProjectManager,
                               ProjectTel = a.ProjectTel,
                               SignTime = a.SignTime,
                               CompletionTime = a.CompletionTime,
                               AcceptanceTime = a.AcceptanceTime,
                               WarrantyTime = a.WarrantyTime,
                               UserManager = a.UserManager,
                               UserTel = a.UserTel,
                               Describe = a.Describe,
                               Address = a.Address,
                               Longitude = a.Longitude,
                               Latitude = a.Latitude,
                           };
                model.obj = linq.FirstOrDefault();
            }
            return model;
        }
        /// <summary>
        /// 根据条件查询项目列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public ReturnListVM<ProInfoModel> GetProList(ProRequest request)
        {
            ReturnListVM<ProInfoModel> list = new ReturnListVM<ProInfoModel>();
            using (CYOADBEntities Entities = new CYOADBEntities())
            {
                var linq = from a in Entities.ProInfo_Tab
                           select new ProInfoModel
                           {
                               ProjectGuid = a.ProjectGuid,
                               ProjectId = a.ProjectId,
                               ProjectName = a.ProjectName,
                               ProjectManager = a.ProjectManager,
                               ProjectTel = a.ProjectTel,
                               SignTime = a.SignTime,
                               CompletionTime = a.CompletionTime,
                               AcceptanceTime = a.AcceptanceTime,
                               WarrantyTime = a.WarrantyTime,
                               UserManager = a.UserManager,
                               UserTel = a.UserTel,
                               Describe = a.Describe,
                               Address = a.Address,
                               Longitude = a.Longitude,
                               Latitude = a.Latitude,
                               AddTime = a.AddTime

                           };
                //模糊查询
                if (!string.IsNullOrEmpty(request.Indistinct))
                {
                    linq = linq.Where(i => i.ProjectName.Contains(request.Indistinct) || i.ProjectId.Contains(request.Indistinct) || i.UserManager.Contains(request.Indistinct));
                }
                ////项目名称条件
                //if (!string.IsNullOrEmpty(request.ProjectName))
                //{
                //    linq = linq.Where(i => i.ProjectName.Contains(request.ProjectName));
                //}
                ////项目编号条件
                //if (!string.IsNullOrEmpty(request.ProjectId))
                //{
                //    linq = linq.Where(i => i.ProjectId.Contains(request.ProjectId));
                //}
                DateTime tmp = DateTime.ParseExact("19000101", "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
                if (request.selStartWarrantyTime != tmp && request.selEndWarrantyTime != tmp)
                {
                    linq = linq.Where(a => a.WarrantyTime >= request.selStartWarrantyTime && a.WarrantyTime <= request.selEndWarrantyTime);
                }
                ////项目联系人条件
                //if (!string.IsNullOrEmpty(request.UserManager))
                //{
                //    linq = linq.Where(i => i.UserManager.Contains(request.UserManager));
                //}
                list.TotalCount = linq.Count();
                list.List = linq.OrderBy(i => i.SignTime).Take(request.PagesSize * request.PageIndex).Skip(request.PagesSize * (request.PageIndex - 1)).ToList();
                foreach (ProInfoModel lim in list.List)
                {

                    int Documentint = Entities.DocumentRelation_Tab.Where(i => i.Type == 1 && i.LPDId == lim.ProjectId).Count();
                    if (Documentint > 0)
                    {
                        lim.IsDocument = true;
                    }
                    else
                    {
                        lim.IsDocument = false;
                    }

                    int Proint = Entities.DevProRelation_Tab.Where(i => i.Code == lim.ProjectId && i.CodeId == "1" && i.LocaId!=null).Count();
                    if (Proint > 0)
                    {
                        lim.IsLoca = true;
                    }
                    else
                    {
                        lim.IsLoca = false;
                    }
                    lim.IsDev = false;
                    List<string> DevIDs = Entities.DevProRelation_Tab.Where(i => i.Code == lim.ProjectId && i.CodeId == "1").Select(i => i.DeviceId).ToList();
                    foreach(string devid in DevIDs)
                    {
                        DevInfo_Tab di = Entities.DevInfo_Tab.Where(i => i.DeviceId == devid && i.State == 1).FirstOrDefault();
                        if (di != null)
                        {

                            lim.IsDev = true;
                        }
                    }


                }
                list.PageIndex = request.PageIndex;
                list.PagesSize = request.PagesSize;
            }
            return list;
        }
        /// <summary>
        /// 通过基站 设备查询项目信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public ReturnListVM<ProInfoExtendModel> GetProExtendList(ProRequest request)
        {
            ReturnListVM<ProInfoExtendModel> list = new ReturnListVM<ProInfoExtendModel>();
            using (CYOADBEntities Entities = new CYOADBEntities())
            {
                var linq = from a in Entities.DevProRelation_Tab.Where(i => i.CodeId == "1")
                           join f in Entities.ProInfo_Tab on a.Code equals f.ProjectId
                           into tmpProInfo
                           let ProInfoTab = tmpProInfo.FirstOrDefault()
                           join d in Entities.DevInfo_Tab on a.DeviceId equals d.DeviceId
                           into tmpDevInfo
                           let DevInfo = tmpDevInfo.FirstOrDefault()
                           select new ProInfoExtendModel
                           {
                               ProjectGuid = ProInfoTab.ProjectGuid,
                               ProjectId = ProInfoTab.ProjectId,
                               ProjectName = ProInfoTab.ProjectName,
                               ProjectManager = ProInfoTab.ProjectManager,
                               ProjectTel = ProInfoTab.ProjectTel,
                               SignTime = ProInfoTab.SignTime,
                               CompletionTime = ProInfoTab.CompletionTime,
                               AcceptanceTime = ProInfoTab.AcceptanceTime,
                               WarrantyTime = ProInfoTab.WarrantyTime,
                               UserManager = ProInfoTab.UserManager,
                               UserTel = ProInfoTab.UserTel,
                               Describe = ProInfoTab.Describe,
                               Address = ProInfoTab.Address,
                               Longitude = ProInfoTab.Longitude,
                               Latitude = ProInfoTab.Latitude,
                               AddTime = ProInfoTab.AddTime,
                               DeviceId = a.DeviceId,
                               LocaId = a.LocaId,
                               DeviceLevel= DevInfo.DeviceLevel


                           };
                //模糊查询
                if (!string.IsNullOrEmpty(request.Indistinct))
                {
                    linq = linq.Where(i => i.ProjectName.Contains(request.Indistinct) || i.ProjectId.Contains(request.Indistinct) || i.UserManager.Contains(request.Indistinct));
                }
                //排除子设备信息
                linq = linq.Where(i => i.DeviceLevel == 1);
                //通过基站编号查询
                if (request.Type == 2)
                {
                    linq = linq.Where(i => i.LocaId == request.LPDId);
                }
                //通过设备编号查询
                if (request.Type == 3)
                {
                    linq = linq.Where(i => i.DeviceId == request.LPDId);
                }
                list.TotalCount = linq.Count();
                list.List = linq.OrderBy(i => i.SignTime).Take(request.PagesSize * request.PageIndex).Skip(request.PagesSize * (request.PageIndex - 1)).ToList();
                list.PageIndex = request.PageIndex;
                list.PagesSize = request.PagesSize;
            }
            return list;
        }
    }
}
